﻿/// <reference path="jquery-1.4.1.js"/>

$.fn.addImageUploader = function (uploadUrl, getWebImageUrl, insertContainerAction) {
    if (!insertContainerAction) {
        insertContainerAction = function (container, sourceDomObj) {
            $(sourceDomObj).after(container);
        };
    };
    return this.each(function () {
        var uploaderContainer = $('<fieldset class="image-uploader">' +
                                      'Preview: ' +
                                      '<div class="qq-upload-spinner image-preview-loading" style="display: none;"></div>' +
                                      '<img class="image-preview" alt="" />' +
                                      '<div class="image-uploader-button"></div>' +
                                      'From URL: <input type="text" maxlength="1000" class="text-box web-image-url" /><br />' +
                                      'Cookies: <textarea class="cookies" /><br />' +
                                      '<input type="button" value="Download image" class="download-image-button" />' +
                                      '<div class="qq-upload-spinner downloading-image" style="display: none;"></div>' +
                                  '</fieldset>');

        insertContainerAction(uploaderContainer, this);
        (function ($) {
            var sourceTextBox = $(this);
            var uploader = new qq.FileUploader({
                // pass the dom node (ex. $(selector)[0] for jQuery users)
                element: $(".image-uploader-button")[0],
                // path to server-side upload script
                action: uploadUrl,
                sizeLimit: 2 * 1024 * 1024,
                onComplete: function (id, fileName, responseJSON) {
                    sourceTextBox.val(responseJSON.fileName).change();
                }
            });
            $(".cookies").val($.jStorage.get("image_uploader_last_used_cookies", ""));
            $(".download-image-button").click(function () {
                var url = $(".web-image-url").val();
                if (!url) {
                    alert("Please enter an URL");
                    return;
                }
                $(".downloading-image").show();
                $(".download-image-button").attr("disabled", "1");
                $(".web-image-url").attr("disabled", "1");
                $(".cookies").attr("disabled", "1");
                var cookies = $(".cookies").val();
                $.jStorage.set("image_uploader_last_used_cookies", cookies);
                $.ajax({
                    type: 'POST',
                    url: getWebImageUrl,
                    dataType: "json",
                    data: {
                        url: url,
                        cookies: cookies
                    },
                    complete: function () {
                        $(".downloading-image").hide();
                        $(".download-image-button").attr("disabled", null);
                        $(".web-image-url").attr("disabled", null);
                        $(".cookies").attr("disabled", null);
                    },
                    success: function (resp) {
                        if (resp.error) {
                            alert(resp.error);
                        } else {
                            $(".web-image-url").val("");
                            sourceTextBox.val(resp.fileName).change();
                        }
                    },
                    error: function () {
                        alert("Not a valid URL");
                    }
                });
            });
            $(".image-preview").load(function () {
                $(".image-preview-loading").hide();
                $(this).show();
            }).error(function () {
                $(".image-preview-loading").hide();
            });
            sourceTextBox.change(function () {
                $(".image-preview").hide();
                var url = $.trim(sourceTextBox.val());
                if (url) {
                    $(".image-preview-loading").show();
                    $(".image-preview").attr("src", sourceTextBox.val());
                }
            }).change();
        }).call(this, $.extend(function () {
            if (arguments.length > 1) {
                return $.apply(this, arguments);
            }
            var query = arguments[0];
            if ((typeof query) === 'string') {
                return $(query, uploaderContainer);
            }
            return $(query);
        }, $)); // replace default context
    });
};
